<template>
  <div class="box">
    <div class="searcharea">
      <!-- 搜索 -->
      <el-row>
        <el-col :span="24">
          <div class="query">
            <el-form :inline="true" :model="queryWorkshopinfoParam" class="demo-form-inline">
              <el-form-item label="车间名称">
                <el-input
                  v-model="queryWorkshopinfoParam.WorkshopName"
                  placeholder="请输入"
                  clearable
                  style="width: 200px"
                />
              </el-form-item>

              <el-form-item>
                <el-button type="primary" :icon="Search" @click="SearchWorkshopinfo()">搜索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="contentarea">
      <!-- 功能按钮 -->
      <el-row>
        <el-col :span="24">
          <div class="functionbutton">
            <el-button type="primary" plain @click="addWorkshopinfo()">添加</el-button>
            <el-button type="danger" plain @click="batchDeletionWorkshopinfo()">批量删除</el-button>
          </div>
        </el-col>
        <el-dialog v-model="centerDialogVisible" title="添加车间" width="30%" align-center>
          <el-form
            ref="CreateWorkshopinfoForm"
            :model="CreateWorkshopinfoForm"
            :rules="CreateWorkshopinfoRules"
            label-width="auto"
            style="padding: 20px"
          >
            <el-form-item label="车间编号">
              <el-input v-model="CreateWorkshopinfoForm.WorkshopNO" placeholder="不输入则自动生成"> </el-input>
            </el-form-item>
            <el-form-item prop="WorkshopName" label="车间名称">
              <el-input v-model="CreateWorkshopinfoForm.WorkshopName" placeholder="请输入车间名称"> </el-input>
            </el-form-item>

            <el-form-item label="负责人" prop="UserId">
              <el-select v-model="CreateWorkshopinfoForm.UserId" filterable placeholder="请选择" clearable>
                <el-option
                  v-for="option in UserinfoOptions"
                  :key="option.key"
                  :label="option.userName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="基地" prop="FoundationId">
              <el-select v-model="CreateWorkshopinfoForm.FoundationId" filterable placeholder="请选择" clearable>
                <el-option
                  v-for="option in FoundationOptions"
                  :key="option.key"
                  :label="option.foundationName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="面积">
              <el-input v-model="CreateWorkshopinfoForm.Area" placeholder="请输入"> </el-input>
            </el-form-item>
          </el-form>

          <template #footer>
            <div class="dialog-footer">
              <el-button @click="centerDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="CreateWorkshopinfoDialog()">确认</el-button>
            </div>
          </template>
        </el-dialog>
      </el-row>

      <!-- 数据表格 -->
      <el-row>
        <el-col :span="24">
          <div>
            <el-table
              v-loading="tableloading"
              :data="tableData"
              border
              style="width: 100%"
              max-height="400px"
              :selection="selectedRows"
              @selection-change="handleSelectionChange"
              @row-click="getRowkey"
              show-overflow-tooltip
              empty-text="暂无数据"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column type="index" label="序号" />
              <el-table-column prop="workshopNo" label="车间编码" width="180" />
              <el-table-column prop="workshopName" label="车间名称" width="180" />
              <el-table-column prop="foundation.foundationName" label="基地" width="220" />
              <el-table-column prop="user.jobNumber" label="工号" />
              <el-table-column prop="user.userName" label="负责人" />
              <el-table-column prop="area" label="面积" />
              <el-table-column prop="creator" label="创建人" />
              <el-table-column prop="updator" label="修改人" />
              <el-table-column prop="createtime" label="创建时间" width="180" :formatter="formatColumnTime" />
              <el-table-column prop="updateDate" label="更新时间" width="180" :formatter="formatColumnTime" />
              <el-table-column fixed="right" label="操作" width="140">
                <template #default="scope">
                  <el-button type="danger" plain size="small" @click="DeleteWorkshopinfoSingle(scope.row.id)">
                    删除
                  </el-button>

                  <el-button type="primary" plain size="small" @click="UpdateWorkshopinfo(scope.row)">更新</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>

        <el-dialog v-model="UpdateDialogVisible" title="修改车间" width="30%" align-center>
          <el-form
            ref="UpdateWorkshopinfoForm"
            :model="UpdateWorkshopinfoForm"
            :rules="CreateWorkshopinfoRules"
            label-width="auto"
            style="padding: 20px"
          >
            <el-form-item label="车间编号">
              <el-input v-model="UpdateWorkshopinfoForm.WorkshopNo" disabled placeholder="不输入则自动生成"> </el-input>
            </el-form-item>
            <el-form-item prop="WorkshopName" label="车间名称">
              <el-input v-model="UpdateWorkshopinfoForm.WorkshopName" placeholder="请输入车间名称"> </el-input>
            </el-form-item>

            <el-form-item label="负责人" prop="UserId">
              <el-select v-model="UpdateWorkshopinfoForm.UserId" filterable placeholder="请选择" clearable>
                <el-option
                  v-for="option in UserinfoOptions"
                  :key="option.key"
                  :label="option.userName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="基地" prop="FoundationId">
              <el-select v-model="UpdateWorkshopinfoForm.FoundationId" filterable placeholder="请选择" clearable>
                <el-option
                  v-for="option in FoundationOptions"
                  :key="option.key"
                  :label="option.foundationName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="面积">
              <el-input v-model="UpdateWorkshopinfoForm.Area" placeholder="请输入"> </el-input>
            </el-form-item>
          </el-form>

          <template #footer>
            <div class="dialog-footer">
              <el-button @click="UpdateDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="UpdateWorkshopinfoDialog()">确认</el-button>
            </div>
          </template>
        </el-dialog>
      </el-row>

      <!-- 分页 -->
      <el-row>
        <el-col :span="24">
          <div class="demo-pagination-block" style="display: flex; justify-content: flex-end">
            <el-config-provider :locale="zhCn">
              <el-pagination
                v-model:current-page="queryWorkshopinfoParam.PageIndex"
                v-model:page-size="queryWorkshopinfoParam.PageSize"
                :page-sizes="[100, 200, 300, 400]"
                :small="false"
                :disabled="false"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="queryWorkshopinfoParam.totalCount"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              >
              </el-pagination>
            </el-config-provider>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script >
import { Search } from '@element-plus/icons-vue'

import { formatTime, capitalizeFirstLetter } from '../../utils/index'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { ElMessageBox, ElNotification } from 'element-plus'
import {
  ApiGetShopfloor_WorkshopinfoPageListAsync,
  ApiDeletionShopfloor_WorkshopinfoAsync,
  ApiCreateShopfloor_WorkshopinfoAsync,
  ApiUpdateShopfloor_WorkshopinfoAsync
} from '../../api/shopfloor/ShopfloorWorkshopinfoApi'
import { ApiGetShopfloor_FoundationPageListAsync } from '../../api/shopfloor/ShopfloorFoundationApi'
import { ApiGetBasicData_UserinfoPageListAsync } from '@/api/basic/basicDataUserinfoApi'
export default {
  name: 'WorkshopinfoView',
  data() {
    return {
      FoundationOptions: [],
      UserinfoOptions: [],
      tableloading: false,
      UpdateDialogVisible: false,
      UpdateWorkshopinfoForm: {},
      CreateWorkshopinfoForm: {},
      CreateWorkshopinfoRules: {
        UserId: [{ required: true, trigger: 'blur', message: '负责人不能为空' }],
        FoundationId: [{ required: true, trigger: 'blur', message: '基地不能为空' }],
        WorkshopName: [{ required: true, trigger: 'blur', message: '车间名称不能为空' }]
      },
      ruleForm: {
        name: ''
      },
      centerDialogVisible: false,
      selectedRows: [], // 存储选中的行数据
      queryWorkshopinfoParam: {
        WorkshopinfoName: null,
        PageIndex: 1,
        PageSize: 100,
        totalCount: 11
      },
      queryUserinfoParam: {
        PageIndex: 1,
        PageSize: 100,
        totalCount: 11
      },
      queryFoundationParam: {
        PageIndex: 1,
        PageSize: 100,
        totalCount: 11
      },
      tableData: [] // 表格数据
    }
  },
  setup() {
    return {
      Search,
      zhCn
    }
  },
  created() {
    this.SearchWorkshopinfo()
  },
  methods: {
    //确认更新
    UpdateWorkshopinfoDialog() {
      this.$refs.UpdateWorkshopinfoForm.validate((valid) => {
        if (valid) {
          ApiUpdateShopfloor_WorkshopinfoAsync(this.UpdateWorkshopinfoForm).then((res) => {
            if (res.data.data) {
              ElNotification({
                message: '更新成功',
                offset: 100,
                type: 'success',
                duration: 2000
              })
              this.UpdateDialogVisible = false
              this.SearchWorkshopinfo()
            }
          })
        }
      })
    },
    //更新车间
    UpdateWorkshopinfo(res) {
      this.UpdateDialogVisible = true
      this.UpdateWorkshopinfoForm = capitalizeFirstLetter(res)
      // 获取负责人信息
      ApiGetBasicData_UserinfoPageListAsync(this.queryUserinfoParam).then((res) => {
        this.UserinfoOptions = res.data.data
        this.queryUserinfoParam.PageIndex = res.data.pageIndex
        this.queryUserinfoParam.PageSize = res.data.pageSize
        this.queryUserinfoParam.totalCount = res.data.totalCount
      })

      // 获取基地信息
      ApiGetShopfloor_FoundationPageListAsync(this.queryFoundationParam).then((res) => {
        this.FoundationOptions = res.data.data
        this.queryFoundationParam.PageIndex = res.data.pageIndex
        this.queryFoundationParam.PageSize = res.data.pageSize
        this.queryFoundationParam.totalCount = res.data.totalCount
      })
    },
    //删除车间
    DeleteWorkshopinfoSingle(res) {
      this.selectedRows = []
      this.selectedRows.push(res)
      this.batchDeletionWorkshopinfo()
    },
    //添加车间
    CreateWorkshopinfoDialog() {
      this.$refs.CreateWorkshopinfoForm.validate((valid) => {
        if (valid) {
          ApiCreateShopfloor_WorkshopinfoAsync(this.CreateWorkshopinfoForm).then((res) => {
            if (res.data.data) {
              ElNotification({
                message: '添加车间成功',
                offset: 100,
                type: 'success',
                duration: 2000
              })
              this.centerDialogVisible = false
              this.SearchWorkshopinfo()
            }
          })
        }
      })
    },

    //获取用户选择项
    handleSelectionChange(select) {
      // 提取传递过来的对象中的"id"字段，并存储到this.selectedRows中
      this.selectedRows = select.map((item) => item.id)
    },
    //添加车间
    addWorkshopinfo() {
      this.CreateWorkshopinfoForm = {}
      this.centerDialogVisible = true

      // 获取负责人信息
      ApiGetBasicData_UserinfoPageListAsync(this.queryUserinfoParam).then((res) => {
        this.UserinfoOptions = res.data.data
        this.queryUserinfoParam.PageIndex = res.data.pageIndex
        this.queryUserinfoParam.PageSize = res.data.pageSize
        this.queryUserinfoParam.totalCount = res.data.totalCount
      })

      // 获取基地信息
      ApiGetShopfloor_FoundationPageListAsync(this.queryFoundationParam).then((res) => {
        this.FoundationOptions = res.data.data
        this.queryFoundationParam.PageIndex = res.data.pageIndex
        this.queryFoundationParam.PageSize = res.data.pageSize
        this.queryFoundationParam.totalCount = res.data.totalCount
      })
    },
    //指删除车间
    batchDeletionWorkshopinfo() {
      if (this.selectedRows.length < 1) {
        ElNotification({
          message: '请先选择一条或更多信息',
          type: 'warning',
          offset: 100,
          duration: 2000
        })
        return
      }
      ElMessageBox.confirm('确实要删除这些信息吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          ApiDeletionShopfloor_WorkshopinfoAsync(this.selectedRows)
            .then((res) => {
              if (res.data.data) {
                ElNotification({
                  message: '删除成功',
                  offset: 100,
                  type: 'success',
                  duration: 2000
                })
                this.selectedRows = []
                this.SearchWorkshopinfo()
              }
            })
            .catch(() => {
              this.selectedRows = []
            })
        })
        .catch(() => {
          this.selectedRows = []
        })
    },
    //时间转换
    formatColumnTime(row, column, time) {
      return formatTime(time)
    },

    //每页条数
    handleSizeChange(e) {
      this.queryWorkshopinfoParam.PageSize = e
      this.SearchWorkshopinfo()
    },

    // 搜索
    SearchWorkshopinfo() {
      this.tableloading = true
      ApiGetShopfloor_WorkshopinfoPageListAsync(this.queryWorkshopinfoParam)
        .then((res) => {
          this.tableData = res.data.data
          this.queryWorkshopinfoParam.PageIndex = res.data.pageIndex
          this.queryWorkshopinfoParam.PageSize = res.data.pageSize
          this.queryWorkshopinfoParam.totalCount = res.data.totalCount
        })
        .finally(() => {
          this.tableloading = false
        })
    }
  }
}
</script>
<style lang="less"  scoped>
@import '../../assets/globle/index.css';
</style>